package com.jiangaifeng.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jiangaifeng.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jiangaifeng.common.annotation.Log;
import com.jiangaifeng.common.core.controller.BaseController;
import com.jiangaifeng.common.core.domain.AjaxResult;
import com.jiangaifeng.common.enums.BusinessType;
import com.jiangaifeng.blog.domain.BlogPost;
import com.jiangaifeng.blog.service.IBlogPostService;
import com.jiangaifeng.common.utils.poi.ExcelUtil;
import com.jiangaifeng.common.core.page.TableDataInfo;

/**
 * postController
 * 
 * @author bug姜
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/blog/post")
public class BlogPostController extends BaseController
{
    @Autowired
    private IBlogPostService blogPostService;

    /**
     * 查询post列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('blog:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogPost blogPost)
    {
        startPage();
        List<BlogPost> list = blogPostService.selectBlogPostList(blogPost);
        return getDataTable(list);
    }

    /**
     * 导出post列表
     */
    @PreAuthorize("@ss.hasPermi('blog:post:export')")
    @Log(title = "post", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogPost blogPost)
    {
        List<BlogPost> list = blogPostService.selectBlogPostList(blogPost);
        ExcelUtil<BlogPost> util = new ExcelUtil<BlogPost>(BlogPost.class);
        util.exportExcel(response, list, "post数据");
    }

    /**
     * 获取post详细信息
     */
    //@PreAuthorize("@ss.hasPermi('blog:post:query')")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(blogPostService.selectBlogPostById(id));
    }

    /**
     * 新增post
     */
    @PreAuthorize("@ss.hasPermi('blog:post:add')")
    @Log(title = "post", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogPost blogPost)
    {
        return toAjax(blogPostService.insertBlogPost(blogPost));
    }

    /**
     * 修改post
     */
    @PreAuthorize("@ss.hasPermi('blog:post:edit')")
    @Log(title = "post", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogPost blogPost)
    {
        return toAjax(blogPostService.updateBlogPost(blogPost));
    }

    /**
     * 删除post
     */
    @PreAuthorize("@ss.hasPermi('blog:post:remove')")
    @Log(title = "post", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(blogPostService.deleteBlogPostByIds(ids));
    }
}
